﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;

namespace det
{
    class Chuoiketnoi
    {
        public SqlConnection con = null;
        public void taoketnoi()
        {
            try
            {
                string chuoi_ket_noi = @"Data Source=TOMMYTEO-PC\SQLEXPRESS;Initial Catalog=DET_THUDUC;Integrated Security=True";
                con = new SqlConnection(chuoi_ket_noi);
                con.Open();
                
            }
            catch
            {
            }
        }
        public bool kiemtraketnoi()
        {
            
            try
            {
                string chuoi_ket_noi = @"Data Source=TOMMYTEO-PC\SQLEXPRESS;Initial Catalog=DET_THUDUC;Integrated Security=True";
                con = new SqlConnection(chuoi_ket_noi);
                con.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void dongketnoi()
        {
            try
            {
               string chuoi_ket_noi = @"Data Source=TOMMYTEO-PC\SQLEXPRESS;Initial Catalog=DET_THUDUC;Integrated Security=True";
               con = new SqlConnection(chuoi_ket_noi);
                con.Close();
            }
            catch
            {
            }
        }
        public bool exc(string sql)
        {
            bool kt;
            SqlCommand cmd = new SqlCommand(sql,con);
            cmd.CommandText = sql;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                return kt = true;
            }
            return false;
        }
        public string phanquyen(string user)
        {
            taoketnoi();
            string sql = "select Quyen from NHAN_VIEN where MANV='" + user + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.CommandText = sql;
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                return reader.GetValue(0).ToString();
            }
            dongketnoi();
            return "0";
            
        }
        public bool ExcuteNonQuery(string SQL, CommandType CT, out string err, params SqlParameter[] pr)
        {
            bool kt = false;
            err = "";
            taoketnoi();
            SqlCommand cmd = new SqlCommand(SQL,con);
            cmd.CommandText = SQL;
            cmd.CommandType = CT;
            cmd.Parameters.Clear();
            foreach (SqlParameter P in pr)
            {
                cmd.Parameters.Add(P);
            }
            try
            {
                cmd.ExecuteNonQuery();
                kt = true;
            }
            catch (SqlException e)
            {
                err = e.Message;
            }
            finally
            {
                dongketnoi();
            }
            return kt;
        }
    }
}
